package cn.yunyichina.reservation.controller.business;

import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.yunyichina.provider.framework.dubbo.service.ReservationService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.reservation.component.qiniu.QiniuService;
import cn.yunyichina.reservation.constant.Console;
import cn.yunyichina.reservation.constant.HttpResponse;
import cn.yunyichina.reservation.controller.BaseResponseController;
import cn.yunyichina.reservation.util.RequestUtil;
import cn.yunyichina.utils.convert.JsonUtils;

/**
 *  前段页面用户：项目规则
 */
@RestController
@RequestMapping("itemRule")
public class ItemRuleController extends BaseResponseController {

    private static final String RESERVATION_CONSOLE_ITEM_RULE_LIST = "yyt.reservation.itemrule.list";
    private static final String RESERVATION_CONSOLE_ITEM_RULE_GET = "yyt.reservation.itemrule.get";
    private static final String RESERVATION_CONSOLE_ITEM_RULE_BY_ITEM_ID_GET = "yyt.reservation.itemrule.by.item.id.get";
    private static final String RESERVATION_CONSOLE_ITEM_RULE_ADD = "yyt.reservation.itemrule.add";
    private static final String RESERVATION_CONSOLE_ITEM_RULE_UPDATE = "yyt.reservation.itemrule.update";
    @Resource
    private QiniuService qiniuService;

    @Resource
    private Properties yamlProperties;

    @GetMapping("itemRules")
    public HttpResponse listItemRule(HttpServletRequest request) {
        Map<String, String> param = RequestUtil.getAllParameters(request);
        Response response = ReservationService.callHumpFromInternal(
                RESERVATION_CONSOLE_ITEM_RULE_LIST, JsonUtils.toJsonString(param)
        );
        return toHttpResponse(response);
    }

    @GetMapping("itemRule")
    public HttpResponse getItemRule(HttpServletRequest request) {
        Map<String, String> param = RequestUtil.getAllParameters(request);
        Response response = ReservationService.callHumpFromInternal(
                RESERVATION_CONSOLE_ITEM_RULE_GET, JsonUtils.toJsonString(param)
        );
        return toHttpResponse(response);
    }
    
    @GetMapping("getItemRuleByItemId")
    public HttpResponse getItemRuleByItemId(HttpServletRequest request) {
        Map<String, String> param = RequestUtil.getAllParameters(request);
        Response response = ReservationService.callHumpFromInternal(
        		RESERVATION_CONSOLE_ITEM_RULE_BY_ITEM_ID_GET, JsonUtils.toJsonString(param)
        );
        return toHttpResponse(response);
    }

    @RequestMapping("addItemRule")
    public HttpResponse addItemRule(HttpServletRequest request) {
        Map<String, String> param = RequestUtil.getAllParameters(request);
        param.put("login_user", JsonUtils.toJsonString(this.getConsoleUser((String) request.getAttribute(Console.USERNAME))));
        Response response = ReservationService.callHumpFromInternal(
                RESERVATION_CONSOLE_ITEM_RULE_ADD, JsonUtils.toJsonString(param)
        );
        return toHttpResponse(response);
    }

    @RequestMapping("updateItemRule")
    public HttpResponse updateItemRule(HttpServletRequest request) {
        Map<String, String> param = RequestUtil.getAllParameters(request);
        param.put("login_user", JsonUtils.toJsonString(this.getConsoleUser((String) request.getAttribute(Console.USERNAME))));
        Response response = ReservationService.callHumpFromInternal(
                RESERVATION_CONSOLE_ITEM_RULE_UPDATE, JsonUtils.toJsonString(param)
        );
        return toHttpResponse(response);
    }

}
